package main

import (
	"fmt"
	"image/png"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/kbinani/screenshot"
	"golang.design/x/hotkey"
)

func capture_save() {
	n := screenshot.NumActiveDisplays()

	for i := 0; i < n; i++ {
		bounds := screenshot.GetDisplayBounds(i)
		img, err := screenshot.CaptureRect(bounds)
		if err != nil {
			panic(err)
		}
		// 指定保存目录
		dir := "D:\\zp\\漏电流截图存放"
		err = os.MkdirAll(dir, os.ModePerm)
		if err != nil {
			fmt.Println("创建目录时出错:", err)
			return
		}

		fileName := get_time()
		fileName += ".png"
		filePath := filepath.Join(dir, fileName)
		file, err := os.Create(filePath)
		err = png.Encode(file, img)
		if err != nil {
			panic(err)
		}
		fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
		defer file.Close()
	}
}

func get_time() string {
	// 获取当前时间
	currentTime := time.Now()
	// 格式化时间为字符串，例如 "2006-01-02_15-04-05"
	fileName := currentTime.Format("2006-01-02_15-04-05")
	return fileName
}

func fn() {
	//hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
	hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyX)
	err := hk.Register()
	if err != nil {
		log.Fatalf("hotkey: failed to register hotkey: %v", err)
		return
	}
	for range hk.Keydown() {
		log.Printf("hotkey: %v is down\n", hk)
		capture_save()
	}
}

//mainthread.Init(fn)
